package org.munoz.shared;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.appengine.api.datastore.PostLoad;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.condition.IfTrue;

@Entity
public class PlayerOnline {
	@Id
	private String userFBId;
	private String name;
	private Set<String> tokens = new HashSet<String>();
	private Set<Key<Match>> matches = new HashSet<Key<Match>>();
	@Index(IfTrue.class)
	private boolean automatchPooled = false;
//	private String userEmail;
	
	// HW#8
	private double rating;
	private double ratingDev; 
	
	public PlayerOnline(){
//		userEmail = "";
//		name = "";
//		tokens = new HashSet<String>();
//		matches = new HashSet<Key<Match>>();
//		automatchPooled = false;
//		rating = 1500;
//		ratingDev = 350;
	}
    
	public PlayerOnline(String fbId, String fbName){// String name) {
//		this.userEmail = userEmail;
		this.userFBId = fbId;
		this.name = fbName;
		//this.tokens = new HashSet<String>();
		//this.matches = new HashSet<Key<Match>>();
		automatchPooled = false;
		rating = 1500;
		ratingDev = 350;
	}

	/**
	 * @return the player's email
	 */
//	public String getUserEmail() {
//		return userEmail;
//	}

	/**
	 * Sets the player's email
	 * @param userEmail
	 */
//	public void setUserEmail(String userEmail) {
//		this.userEmail = userEmail;
//	}

	public String getUserFBId() {
		return userFBId;
	}

	public void setUserFBId(String userFBId) {
		this.userFBId = userFBId;
	}

	/**
	 * @return the name of the player
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name of the player
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the tokens of the player
	 */
	public Set<String> getTokens() {
		return tokens;
	}
	
	/**
	 * Adds tokens to the player
	 * @param token
	 */
	public void addToken(String token) {
	    tokens.add(token);
	}

	/**
	 * Remove tokens from the player's list
	 * @param token
	 */
	public void removeToken(String token) {
		System.out.println("Token was removed from player " + userFBId);// userEmail);
	    tokens.remove(token);
	}
	  
	/**
	 * @return the matches in which the player is active. 
	 */
	public Set<Key<Match>> getMatches() {
		return matches;
	}

	/**
	 * Set the matches in which the player is active
	 * @param matches
	 */
	public void setMatches(Set<Key<Match>> matches) {
		this.matches = matches;
	}
	
	/**
	 * Removes a match from the matches list
	 * @param match
	 */
	public void removeMatch(Key<Match> match) {
	    matches.remove(match);
    }
	
	/**
	 * Used for automatch 
	 * @param condition
	 */
	public void setAutomatchEligible(boolean condition) {
	    automatchPooled = condition;
	}
	
	/**
	 * Adds a match to the list of matches
	 * @param match
	 */
	public void addMatch(Key<Match> match) {
	    matches.add(match);
	}
	
	/**
	 * @return the rating of the player
	 */
	public double getRating() {
		return rating;
	}

	/**
	 * Sets the rating of the player
	 * @param rating 
	 */
	public void setRating(double rating) {
		this.rating = rating;
	}

	/**
	 * @return the rating deviation
	 */
	public double getRatingDev() {
		return ratingDev;
	}

	/**
	 * Sets the rating deviation
	 * @param ratingDev
	 */
	public void setRatingDev(double ratingDev) {
		this.ratingDev = ratingDev;
	}

	/**
	 * @param match
	 * @return true if match is in the list of matches.
	 */
	public boolean containsMatchKey(Key<Match> match) {
	    return matches.contains(match);
	}
}

